/**   
* @Title: JBeerAop.java
* @Package com.jbeer.framework
* @author Bieber
* @date 2014-5-18 下午8:06:17
* @version V1.0   
*/

package com.jbeer.framework;

import java.lang.reflect.Method;
import java.util.Set;

import com.jbeer.framework.annotation.AOP;
import com.jbeer.framework.annotation.After;
import com.jbeer.framework.annotation.Before;
import com.jbeer.framework.annotation.Excep;
import com.jbeer.framework.aop.JBeerAopContext;
import com.jbeer.framework.constant.JBeerConstant;
import com.jbeer.framework.enumeration.BeanScope;
import com.jbeer.framework.exception.InitializationException;
import com.jbeer.framework.exception.ScanClassException;
import com.jbeer.framework.ioc.JBeerIOCContainerContext;
import com.jbeer.framework.utils.ClassUtils;

/**
 * <p>类功能说明:aop模块</p>
 * <p>类修改者	    修改日期</p>
 * <p>修改说明</p>
 * <p>Title: JBeerAop.java</p>
 * @author Bieber <a mailto="bieber.jbeer@hotmail.com">bieber.jbeer@hotmail.com</a>
 * @date 2014-5-18 下午8:06:17
 * @version V1.0
 */

public class JBeerAop{

	public static void init() throws ScanClassException, InitializationException{
		Set<Class<?>> classSet = ClassUtils.scanClassesByAnnotation(AOP.class);
		if(classSet==null||classSet.size()<=0){
			return ;
		}
		for(Class<?> clazz:classSet){
			Method[] methods = clazz.getDeclaredMethods();
			AOP aop = clazz.getAnnotation(AOP.class);
			Method beforeM=null;
			Method afterM = null;
			Method excepM = null;
			for(Method method:methods){
				Before before = method.getAnnotation(Before.class);
				After after = method.getAnnotation(After.class);
				Excep excep = method.getAnnotation(Excep.class);
				if(before!=null){
					beforeM=method;
				}
				if(after!=null){
					afterM=method;
				}
				if(excep!=null){
					excepM=method;
				}
			}
			JBeerAopContext.registAspectBean(aop.classRegex(),aop.methodRegex(),aop.argTypes(),aop.handleException(),clazz.getName(), beforeM, afterM, excepM);
			JBeerIOCContainerContext.registBeanDefinition(clazz, JBeerConstant.IOC_DEFALUT_FACTORY_METHOD_NAME, BeanScope.SINGLETON, clazz.getName());
		}
	}

}
